﻿
namespace Jayden.Dll.Tetris
{
	public class Block
	{
		private BlockDefinition m_Definition;
		private int m_Orientation;
		private int m_Index;
		private int m_X;
		private int m_Y;

		public const int NORTH = 0;
		public const int WEST = 1;
		public const int SOUTH = 2;
		public const int EAST = 3;

		public Block(BlockDefinition definition)
		{
			m_Definition = definition;
		}

		public BlockDefinition Definition { get { return m_Definition; } }

		public int X { get { return m_X; } set { m_X = value; } }
		public int Y { get { return m_Y; } set { m_Y = value; } }

		public int Orientation { get { return m_Orientation; } set { m_Orientation = value; } }
		public int RotateRight() { if (m_Orientation > 3) return 0; return m_Orientation + 1; }
		public int RotateLeft() { if (m_Orientation < 0) return 3; return m_Orientation - 1; }

		public int Width { get { return (Orientation & 1) == 0 ? m_Definition.Width : m_Definition.Height; } }
		public int Height { get { return (Orientation & 1) == 0 ? m_Definition.Height : m_Definition.Width; } }

		public bool IsBlock(int x, int y)
		{
			if (x < m_X || x >= m_X + Width || y < m_Y || y >= m_Y + Height)
				return false;
			int rx = x - m_X;
			int ry = y - m_Y;
			if (Orientation == WEST)
				return m_Definition.IsBlock(ry, m_Definition.Height - (rx + 1));
			if (Orientation == SOUTH)
				return m_Definition.IsBlock(m_Definition.Width - (rx + 1), m_Definition.Height - (ry + 1));
			if (Orientation == EAST)
				return m_Definition.IsBlock(m_Definition.Width - (ry + 1), rx);
			return m_Definition.IsBlock(rx, ry);
		}

	}
}
